/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root == null){
            return true;
        }
        int dl = deepth(root.left);
        int dr = deepth(root.right);
        boolean res = Math.abs(dl - dr) <= 1;
        return isBalanced(root.left) && isBalanced(root.right) && res;
    }
    private int deepth(TreeNode node){
        if(node == null){
            return  0;
        }
        return Math.max(deepth(node.left),deepth(node.right)) + 1;
    }
}